﻿<%@ Page Title="Quản lý người dùng" Language="C#" MasterPageFile="~/OneColumn.Master"
    AutoEventWireup="true" CodeBehind="UserManagement.aspx.cs" Inherits="KCL.Organization.UserManagement" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageHeader" runat="server">
    <link type="text/css" href="../styles/themes/base/jquery.ui.all.css" rel="stylesheet" />
    <style type="text/css">
        #password2 + span
        {
            display: none;
        }
    </style>
    <script type="text/javascript" src="../styles/ui/jquery.ui.core.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.position.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.mouse.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.draggable.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.dialog.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.button.js"></script>
    <script type="text/javascript" src="../styles/external/jquery.bgiframe-2.1.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#divCreateUser").dialog({
                autoOpen: false,
                modal: true,
                width: 500,
                height: 'auto',
                maxHeight: 900,
                title: 'Nhập thông tin người dùng',
                resizable: false
            });
            $("#btnSave").button();

            $("#password2").blur(function () {
                Validate();
            });
        });

        function Validate() {
            var password1 = $("#myPassword").val();
            var password2 = $("#password2").val();
            if (password1 != password2) {
            	$("#password2 + span").css("display", "inline").fadeOut(5000);
            	return false;
            }
            return true;
        }

        function SubmitUser() {
            if (!Validate()) return;
            $.ajax({
                url: "UserManagement.ashx?",
                type: "POST",
                dataType: "script",
                data: {
                    username: $("#myUsername").val(),
                    password: $("#myPassword").val(),
                    fullname: $("#fullname").val(),
                    email: $("#email").val(),
                    admin: ($("#admin").attr("checked") ? 1 : 0),
                    create: ($("#create").attr("checked") ? 1 : 0),
                    edit: ($("#edit").attr("checked") ? 1 : 0),
                    id: $("#UserId").val(),
                    action: $("#action").val()
                }
            });            
        }
        function DisplayUser(id) {
            var currentRow = $("#link" + id).parent().parent().children();
            $("#myUsername").val($(currentRow[0]).html());
            $("#fullname").val($(currentRow[1]).html());
            $("#email").val($(currentRow[2]).html());
            $("#admin").attr("checked", $("#cbAdmin" + id).attr("checked"));
            $("#create").attr("checked", $("#cbCreate" + id).attr("checked"));
            $("#edit").attr("checked", $("#cbEdit" + id).attr("checked"));
            $("#action").val("edit");
            $("#UserId").val(id);
            $("#divCreateUser").dialog("open");
        }

        function DeleteUser(UserID) {
            $.ajax({
                url: "UserManagement.ashx?",
                type: "POST",
                dataType: "script",
                data: {
                    id: UserID,                    
                    action: "delete"
                }
            }); 
        }

        function RefreshUser(id) {
            var currentRow = $("#link" + id).parent().parent().children();
            $(currentRow[0]).html($("#UserName").val());
            $(currentRow[1]).html($("#address").val());
            $(currentRow[2]).html($("#contactAddress").val());
            $(currentRow[3]).html($("#contactPerson").val());
            $(currentRow[4]).html($("#phone").val());
            $(currentRow[5]).html($("#mobile").val());
            $(currentRow[6]).html($("#fax").val());
            $(currentRow[7]).html($("#accountNumber").val());
            $(currentRow[8]).html($("#bank").val());
            $(currentRow[9]).html($("#taxNumber").val());
            $(currentRow[10]).html($("#director").val());
        }
        function PopupAddNewUser() {
            $("#myUsername").val("");
            $("#myPassword").val("");
            $("#fullname").val("");
            $("#email").val("");
            $("#admin").attr("checked",false);
            $("#create").attr("checked", false);
            $("#edit").attr("checked", false);
            $('#action').val('create'); 
            $('#divCreateUser').dialog('open')
        }

    </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ObjectDataSource ID="dsUser" TypeName="KCL.Code.Data.UserDAO" SortParameterName="sortExpression"
        SelectMethod="GetUsers" SelectCountMethod="GetTotalUser" EnablePaging="true"
        runat="server"></asp:ObjectDataSource>
    <table>
        <tr>
            <td>
                <input type="button" id="btnCreateUser" value="Thêm người dùng" onclick="PopupAddNewUser()" />
            </td>
        </tr>
    </table>
    <asp:GridView ID="gvUser" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false"
        BackColor="Wheat" BorderColor="Firebrick" BorderStyle="Ridge" BorderWidth="1px"
        CellPadding="7" DataSourceID="dsUser" GridLines="None" Width="100%" runat="server">
        <HeaderStyle BackColor="Black" ForeColor="Azure" Font-Bold="true" Wrap="false" />
        <FooterStyle ForeColor="Maroon" BackColor="DarkGoldenrod" />
        <PagerStyle ForeColor="Snow" HorizontalAlign="Right" BackColor="SaddleBrown" />
        <RowStyle BackColor="Ivory" />
        <Columns>
            <asp:BoundField HeaderText="Tên đăng nhập" DataField="username" SortExpression="username" />
            <asp:BoundField HeaderText="Tên đầy đủ" DataField="fullname" SortExpression="fullname" />
            <asp:BoundField HeaderText="Email" DataField="email" SortExpression="email" />
            <asp:BoundField HeaderText="Mã nv" DataField="code" SortExpression="code" />
            <asp:TemplateField HeaderText="Phân quyền">
                <ItemTemplate>
                    <input type="checkbox" checked="checked" disabled="disabled" />
                    Xem
                    <input type="checkbox" id='<%# "cbCreate" + Eval("id") %>' <%# Convert.ToBoolean(Eval("create")) ? "checked='checked'" : ""%> disabled="disabled" />
                    Thêm mới
                    <input type="checkbox" id='<%# "cbEdit" + Eval("id") %>' <%# Convert.ToBoolean(Eval("edit")) ? "checked='checked'" : ""%> disabled="disabled" />
                    Chỉnh sửa
                    <input type="checkbox" id='<%# "cbAdmin" + Eval("id") %>' <%# Convert.ToBoolean(Eval("admin")) ? "checked='checked'" : ""%> disabled="disabled" />
                    Quản trị
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Quản lý">
                <ItemTemplate>
                    <a href="javascript:DisplayUser(<%# Eval("id")%>)" id="link<%# Eval("id")%>">
                        <img src="../styles/images/edit.png" alt="Chỉnh sửa" style="border:0;width:16px" /></a>

                    <a href="javascript:DeleteUser(<%# Eval("id")%>)" id='<%# "btnDelete" + Eval("id") %>'>
                        <img src="../styles/images/cancel.png" alt="Xóa" style="border:0;width:16px" /></a>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <AlternatingRowStyle BackColor="SeaShell" />
    </asp:GridView>
    <i>Đang xem trang
        <%=gvUser.PageIndex + 1%>
        /
        <%=gvUser.PageCount %></i>
    <div id="divCreateUser">
        <table border="0" cellpadding="4px" cellspacing="0px">
            <tr>
                <td>
                    Tên đăng nhập
                </td>
                <td>
                    <input type="text" id="myUsername" />
                </td>
            </tr>
            <tr>
                <td>
                    Mật khẩu
                </td>
                <td>
                    <input type="password" id="myPassword" />
                </td>
            </tr>
            <tr>
                <td>
                    Nhập lại mật khẩu
                </td>
                <td>
                    <input type="password" id="password2" /><span style="color: Red;">&nbsp;&nbsp;&nbsp;Mật
                        khẩu không khớp!</span>
                </td>
            </tr>
            <tr>
                <td>
                    Tên đầy đủ
                </td>
                <td>
                    <input type="text" id="fullname" />
                </td>
            </tr>
            <tr>
                <td>
                    Email
                </td>
                <td>
                    <input type="text" id="email" />
                </td>
            </tr>
            <tr>
                <td>
                    Phân quyền
                </td>
                <td>
                    <input type="checkbox" id="read" checked="checked" disabled="disabled" />
                    Xem
                    <input type="checkbox" id="create" />
                    Thêm mới
                    <input type="checkbox" id="edit" />
                    Chỉnh sửa
                    <input type="checkbox" id="admin" />
                    Quản trị
                </td>
            </tr>
        </table>
        <p />
        <center>
            <input type="button" id="btnSave" value="&nbsp;Lưu&nbsp;" onclick="SubmitUser()" /></center>
        <input type="hidden" value="create" id="action" />
        <input type="hidden" value="-1" id="UserId" />
    </div>
</asp:Content>
